package com.example.modelfunction.test.jpatest;


import com.example.base.RequestQuery;
import com.example.base.Redis.RedisAccessManager;
import com.example.modelfunction.shrio.usermanager.mode.User;
import com.example.modelfunction.shrio.usermanager.repository.UserRepository;
import com.example.base.SpringContextHolder;
import org.hibernate.SessionFactory;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.SingleTableEntityPersister;
import org.hibernate.persister.walking.spi.AttributeDefinition;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import javax.persistence.EntityManagerFactory;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.net.URL;
import java.util.*;

@RestController
public class UserAction {
    @Autowired
    private UserRepository userMapper;
    @Autowired
    RedisAccessManager redis;
    @Autowired
    private EntityManagerFactory emf;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String helloSpringBoot(){
        return "Hello SpringBoot!";
    }

    @RequestMapping(value = "/emf",method = RequestMethod.GET)
    public String emf() {
       EntityManagerFactory entityManagerFactory = SpringContextHolder.getBean(EntityManagerFactory.class);
        SessionFactoryImpl sessionFactory = (SessionFactoryImpl) entityManagerFactory.unwrap(SessionFactory.class);
        StringBuffer buffer=new StringBuffer();
        ClassMetadata metadata=sessionFactory.getClassMetadata(User.class);
        String []minfo=metadata.getPropertyNames();
          Map<String, EntityPersister> persisterMap = sessionFactory.getEntityPersisters();
        for (Map.Entry<String, EntityPersister> entity : persisterMap.entrySet()) {
            Class targetClass = entity.getValue().getMappedClass();
            SingleTableEntityPersister persister = (SingleTableEntityPersister) entity.getValue();
            Iterable<AttributeDefinition> attributes = persister.getAttributes();
            String entityName = targetClass.getSimpleName();//Entity的名称
            String tableName = persister.getTableName();//Entity对应的表的英文名
            buffer.append("类名：" + entityName + " => 表名：" + tableName+" ");
            System.out.println("类名：" + entityName + " => 表名：" + tableName);

            //属性
            for (AttributeDefinition attr : attributes) {
                String propertyName = attr.getName(); //在entity中的属性名称
                String[] columnName = persister.getPropertyColumnNames(propertyName); //对应数据库表中的字段名
                String type = "";
                PropertyDescriptor targetPd = BeanUtils.getPropertyDescriptor(targetClass, propertyName);
                if (targetPd != null) {
                    type = targetPd.getPropertyType().getSimpleName();
                }
                buffer.append( type +"：" +propertyName + "：" + ":" + columnName[0]);
            }

        }

        return buffer.toString();
    }

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public List<User> getUserList(){
        Enumeration<URL> mesg=null;
        ClassLoader loader = this.getClass().getClassLoader();
        try {
              mesg = loader.getResources("META-INF/spring.factories");
        } catch (IOException ex) {
            throw new IllegalArgumentException("Unable to load factories from location [" +
                    "META-INF/spring.factories" + "]", ex);
        }
  /*      // Pageable pageable = PageRequest.of(0,500);
        // Page<User> users= userMapper.findAll(pageable);*/
        RequestQuery query=new RequestQuery();
        query.addQueryParam("account","13612345678")
         .addQueryParam("accessToken","abcde");
        return userMapper.findByRequestQueryOr(query);
    }



    @RequestMapping(value = "/userall",method = RequestMethod.GET)
    public List<User> getUserListAll(){


  /*      // Pageable pageable = PageRequest.of(0,500);
        // Page<User> users= userMapper.findAll(pageable);*/
     /*   RequestQuery query=new RequestQuery();
        query.addQueryParam("account","13415000094");
        Page<User> users= userMapper.findByRequestQuery(query);*/

        return userMapper.findALLSql();
    }
    @RequestMapping(value = "/userlike",method = RequestMethod.GET)
    public List<User> getUserlike(String key,String value){


  /*      // Pageable pageable = PageRequest.of(0,500);
        // Page<User> users= userMapper.findAll(pageable);*/
     /*   RequestQuery query=new RequestQuery();
        query.addQueryParam("account","13415000094");
        Page<User> users= userMapper.findByRequestQuery(query);*/

        return userMapper.findObjectLike(key,value+'%');
    }

    @RequestMapping(value = "/usermeta",method = RequestMethod.GET)
    public String[] getUser(String token){
       User user= userMapper.findByAccount(token);
       return user.metaInfo();
    }
    /*
     * @Author libo
     * @Description //fastjson转换对象
     * @Date 上午 11:47 2019/6/5 0005
     * @Param [token]
     * @return java.util.List<java.lang.String>
    */
    @RequestMapping(value = "/userString",method = RequestMethod.GET)
    public String getUserString(String token){
        User user= userMapper.findByAccount(token);
        String res=user.toString();
        return  res;
    }
    /*
     * @Author libo
     * @Description //注解获取对象的可见属性
     * @Date 上午 11:46 2019/6/5 0005
     * @Param 
     * @return 
    */
    @RequestMapping(value = "/uservisiable",method = RequestMethod.GET)
    public List<String> getUservisiable(String token){
        User user= userMapper.findByAccount(token);
        List<String> res=new ArrayList<String>();
        SpringContextHolder.visiablePropertys(user.getClass(),res);
        return res;
    }
    @RequestMapping(value = "/usertel",method = RequestMethod.GET)
    public User getUserTel(String token){
        return userMapper.findByTel(token);
    }


}